package com.sf.adapter;

import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.sf.pathmemorymethod.PileEditActivity;
import com.sf.pathmemorymethod.R;
import com.sf.util.Constant;
import com.sf.util.Mysqlitehelper;
import com.sf.util.Path;
import com.sf.util.Pile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by YSir on 2018/7/7.
 */
public class PileEditAdapter extends BaseAdapter {
    private Context context;                        //运行上下文
    private List<Pile> mData;    //路径集合
    private LayoutInflater mInflater;   //视图容器

    private Mysqlitehelper helper;

    public PileEditAdapter(Context context,List<Pile> mData){
        this.context = context;
        this.mData = mData;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private final class ViewHolder{
        private int id;
        private TextView sort;
        private TextView name;
        private Button btn_update;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final Pile pile = mData.get(position);

        ViewHolder holder = null;

        if(convertView == null){
            convertView = mInflater.inflate(R.layout.list_item_pile_edit, null);
            holder = new ViewHolder();
            holder.id = pile.getId();
            holder.sort = (TextView)convertView.findViewById(R.id.tv_sort);
            holder.name = (TextView)convertView.findViewById(R.id.tv_name);
            holder.btn_update = (Button) convertView.findViewById(R.id.btn_update);

            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        holder.name.setText(pile.getName());
        holder.sort.setText(pile.getSort()+"");

        final ViewHolder finalHolder = holder;
        holder.btn_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showInfo(pile.getId(),pile.getName());

            }
        });

        return convertView;

    }

    public void showInfo(final int id, String name){

        // 创建对话框构建器
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        // 获取布局
        View view2 = View.inflate(context, R.layout.layout_add_pile, null);
        // 获取布局中的控件
        final EditText editname = (EditText) view2.findViewById(R.id.ed_name);
        editname.setText(name);
        final Button button = (Button) view2.findViewById(R.id.btn_edit);
        // 设置参数
        builder.setTitle("更改名称")
                .setView(view2);
        // 创建对话框
        final AlertDialog alertDialog = builder.create();
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub


                helper = new Mysqlitehelper(context);
                SQLiteDatabase db = helper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put(Constant.NAME,editname.getText().toString()+""); //键值对 put(字段名称,值)
                String[] args = {String.valueOf(id)};
                long result = db.update(Constant.TABLE_PILE,values,"id=?",args);
                if (result>0){
                    Toast.makeText(context, "更新成功", Toast.LENGTH_SHORT).show();
                }

                // 刷新页面
                Intent intent = new Intent();
                intent.setAction("action.refreshActivity");
                context.sendBroadcast(intent);
                alertDialog.dismiss();// 对话框消失
            }

        });
        alertDialog.show();

    }
}
